HEX
Server: Apache/2.4.68 (Debian)
System: Linux as-cs-widget-demo-us-central1 6.1.0-44-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.164-1 (2026-03-09) x86_64
User: root (0)
PHP: 8.2.32
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/wp-recipe-maker/assets/js/shared/Api/Modal.js
const modalEndpoint = wprm_admin.endpoints.modal;

import ApiWrapper from '../ApiWrapper';

const rateLimit = 500;

let gettingSuggestions = false;
let gettingSuggestionsAt = false;
let gettingSuggestionsNextArgs = false;

export default {
    getSuggestions(args) {
        if ( ! gettingSuggestions ) {
            return this.getSuggestionsDebounced(args);
        } else {
            gettingSuggestionsNextArgs = args;
            return new Promise(r => r(false));
        }
    },
    getSuggestionsDebounced(args) {
        gettingSuggestions = true;
        const now = Date.now();
        
        if ( false !== gettingSuggestionsAt && rateLimit > now - gettingSuggestionsAt ) {
            return new Promise(r => {
                setTimeout(() => {
                    r( this.getSuggestionsDebounced( args ) );
                }, now - gettingSuggestionsAt );
            });
        }

        gettingSuggestionsAt = now;

        return ApiWrapper.call( `${modalEndpoint}/suggest`, 'POST', args ).then(json => {
            // Check if another request is queued.
            if ( gettingSuggestionsNextArgs ) {
                const newArgs = gettingSuggestionsNextArgs;
                gettingSuggestionsNextArgs = false;

                return this.getSuggestionsDebounced(newArgs);
            } else {
                // Return this request.
                gettingSuggestions = false;
                return json;
            }
        });
    },
};